
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       sim_plane.h
  * @author     baiyang
  * @date       2022-12-9
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include "sim_aircraft.h"
#include "sim_icengine.h"

#include <stdint.h>
#include <stdbool.h>

#include <common/gp_math/gp_mathlib.h>
/*-----------------------------------macro------------------------------------*/
#define PLANE_COEFFICIENT_DEFAULT { \
    .s = 0.45f,                     \
    .b = 1.88f,                     \
    .c = 0.24f,                     \
    .c_lift_0 = 0.56f,              \
    .c_lift_deltae = 0,             \
    .c_lift_a = 6.9f,               \
    .c_lift_q = 0,                  \
    .mcoeff = 50,                   \
    .oswald = 0.9f,                 \
    .alpha_stall = 0.4712f,         \
    .c_drag_q = 0,                  \
    .c_drag_deltae = 0.0f,          \
    .c_drag_p = 0.1f,               \
    .c_y_0 = 0,                     \
    .c_y_b = -0.98f,                \
    .c_y_p = 0,                     \
    .c_y_r = 0,                     \
    .c_y_deltaa = 0,                \
    .c_y_deltar = -0.2f,            \
    .c_l_0 = 0,                     \
    .c_l_p = -1.0f,                 \
    .c_l_b = -0.12f,                \
    .c_l_r = 0.14f,                 \
    .c_l_deltaa = 0.25f,            \
    .c_l_deltar = -0.037f,          \
    .c_m_0 = 0.045f,                \
    .c_m_a = -0.7f,                 \
    .c_m_q = -20,                   \
    .c_m_deltae = 1.0f,             \
    .c_n_0 = 0,                     \
    .c_n_b = 0.25f,                 \
    .c_n_p = 0.022f,                \
    .c_n_r = -1,                    \
    .c_n_deltaa = 0.00f,            \
    .c_n_deltar = 0.1f,             \
    .deltaa_max = 0.3491f,          \
    .deltae_max = 0.3491f,          \
    .deltar_max = 0.3491f,          \
    .CGOffset = {.x=-0.15f, .y=0, .z=-0.05f} \
}
/*----------------------------------typedef-----------------------------------*/
typedef struct sim_plane* sim_plane_t;
typedef const struct sim_plane* sim_plane_const_t;

struct plane_coefficient {
    // from last_letter skywalker_2013/aerodynamics.yaml
    // thanks to Georacer!
    float s;
    float b;
    float c;
    float c_lift_0;
    float c_lift_deltae;
    float c_lift_a;
    float c_lift_q;
    float mcoeff;
    float oswald;
    float alpha_stall;
    float c_drag_q;
    float c_drag_deltae;
    float c_drag_p;
    float c_y_0;
    float c_y_b;
    float c_y_p;
    float c_y_r;
    float c_y_deltaa;
    float c_y_deltar;
    float c_l_0;
    float c_l_p;
    float c_l_b;
    float c_l_r;
    float c_l_deltaa;
    float c_l_deltar;
    float c_m_0;
    float c_m_a;
    float c_m_q;
    float c_m_deltae;
    float c_n_0;
    float c_n_b;
    float c_n_p;
    float c_n_r;
    float c_n_deltaa;
    float c_n_deltar;
    float deltaa_max;
    float deltae_max;
    float deltar_max;
    // the X CoG offset should be -0.02, but that makes the plane too tail heavy
    // in manual flight. Adjusted to -0.15 gives reasonable flight
    Vector3f_t CGOffset;
};

struct sim_plane {
    struct sim_aircraft aircraft;

    float hover_throttle;       // default, 0.7f
    float air_density;          // default, 1.225f, kg/m^3 at sea level, ISA conditions
    float angle_of_attack;
    float beta;

    struct plane_coefficient coefficient;

    float thrust_scale;
    bool reverse_thrust;
    bool elevons;
    bool vtail;
    bool dspoilers;
    bool reverse_elevator_rudder;
    bool ice_engine;
    bool tailsitter;
    bool aerobatic;
    bool copter_tailsitter;
    bool have_launcher;
    float launch_accel;
    float launch_time;
    uint64_t launch_start_ms;

    uint8_t throttle_servo;       // default, 2
    int8_t choke_servo;           // default, 14;
    int8_t ignition_servo;        // default, 12;
    int8_t starter_servo;         // default, 13;
    float slewrate;               // default, 100;

    struct sim_icengine icengine;
};
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
void sim_plane_ctor(sim_plane_t plane, sim_aircraft_ops_t ops, const char *frame_str);
sim_aircraft_t sim_plane_create(const char *frame_str);
void sim_plane_calculate_forces(sim_plane_t plane, const struct sitl_input *input, Vector3f_t *rot_accel);
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



